import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zhourh on 2018/6/12.
 */
public class BinaryTreePaths {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(5);

        System.out.println(new BinaryTreePaths().binaryTreePaths(root));
    }

    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) {
            return Collections.emptyList();
        }

        String rootPath = String.valueOf(root.val);
        List<String> paths = new ArrayList<>();
        continueMove(root, paths, rootPath);
        return paths;
    }

    public void continueMove(TreeNode treeNode, List<String> paths, String path) {
        if (treeNode == null) {
            return;
        }

        if (treeNode.left == null && treeNode.right == null) {
            //已经达到叶子节点
            paths.add(path);
            return;
        }

        if (treeNode.left != null) {
            continueMove(treeNode.left , paths, path + "->" + treeNode.left.val);
        }

        if (treeNode.right != null) {
            continueMove(treeNode.right, paths, path + "->" + treeNode.right.val);
        }
    }
}
